var app = getApp();
Page({
    data:{
    	curPage : '/zofui_sales/pages/page/page',
        isIPX : app.globalData.isIPX ? 1 : 0,
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        Time : app.globalData.Time,
        ispage : true,
        downget : false,
        page : {
            isend : false,
            doing : false,
            doo : 'index_v1',
            pdata : {
                op : 'list',
                page : 1,
                otype : 0,
                stype : 0,
                gtype : 0,
                sortid : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        shopid : '',
    },
	onShow: function () {
		wx.hideTabBar();
        console.log(new Date() + '进入页面');
	},
    onLoad:function(options){
        var self = this;
        
        if( options.scene ){
            var list = options.scene.split('_');
            options.id = list[0] ? list[0] : 0;
            options.shopid = list[1] ? list[1] : 0;
        }
        var shopid = options.shopid ? options.shopid : 0;

        self.setData({
            options : options,
            zfid : options.zfid ? options.zfid : 0,
            pid : options.id ? options.id : 0,
            statusBarHeight : app.globalData.sysInfo ? app.globalData.sysInfo.statusBarHeight : 20,
        });
        
        var loc = wx.getStorageSync('nlloc');
        if( loc.latitude ){
            self.setData({
                lat : loc.latitude,
                lng : loc.longitude,
                'page.pdata.lat' : loc.latitude,
                'page.pdata.lng' : loc.longitude,
                locname : loc.name ? loc.name : false,
            });
        }

		app.com.auth(function(){
			self.setData({
				isauth : true,
			})
			self.getData(loc.latitude,loc.longitude,false);
			if( self.data.zfid > 0 ){
				app.com.http('user_v1','POST',{op:'auth',zfid:self.data.zfid},0,false,'','',function(res){})
			}
			
			app.com.setBar(app,self,shopid);
		});
        

    },
    getData(lat,lng,hidestate,fn){
        var self = this;
        var lat = lat ? lat : 11;
        var lng = lng ? lng : 11;
        var cachetime = self.data.isdown ? 0 : 30;
        app.com.http('index_v1','POST',{op:'info',lat:lat,lng:lng,zfid:self.data.zfid,pid:self.data.pid,hidestate:hidestate},cachetime,false,'','',function(res){
            if (!res.data.errno) {

                if(res.data.data.params.isheader == 1){
                    var weburl = encodeURIComponent( res.data.data.params.headerurl );
                    wx.navigateTo({
                        url: '/zofui_sales/pages/webview/webview?url='+weburl,
                    });
                }

                self.setData({
                	params : res.data.data.params,
                    pagedata : res.data.data.page,
                    sharedata : res.data.data.sharedata,
                    buyed : res.data.data.buyed ? res.data.data.buyed : false,
                    curPage : '/zofui_sales/pages/page/page?id='+self.data.pid +'&shopid=0',
                },function(){
                	console.log(new Date() + '展示页面');

                    if(fn) fn();

                    self.setData({
                        inited : true,
                    });
                    self.swiperchange();
                });

                wx.setNavigationBarTitle({
                  title:  res.data.data.params.title ? res.data.data.params.title : '主页',
                });

		        
                // 关闭下拉刷新
                wx.stopPullDownRefresh();

                if( !hidestate ){
                    app.com.getLocation(self,function(loc){
                        self.setData({
                            lat : loc.latitude,
                            lng : loc.longitude,
                            'page.pdata.lat' : loc.latitude,
                            'page.pdata.lng' : loc.longitude,
                            locname : loc.name ? loc.name : false,
                        });
                    },1);

                    for (var i = 0; i < res.data.data.page.length; i++) {
                        var thisd = res.data.data.page[i];
                        if( thisd.name == 'downget' ){
                            self.onReachBottom();
                            return false;
                        }
                    }
                }
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });

        var cityinfo = this.selectComponent('#cityinfo');
        if(cityinfo) cityinfo.reset('down');

        var pdownget = this.selectComponent('#pdownget');
        if(pdownget) pdownget.reset('down');
        
        for (var i = 0; i < this.data.pagedata.length; i++) {
            if(this.data.pagedata[i].name == 'article'){
                let articlelist = this.selectComponent('#articlelist'+i);
                if(articlelist) articlelist.reset('down');
            }else if(this.data.pagedata[i].name == 'shop'){
                let shoplist = this.selectComponent('#shop'+i);
                if(shoplist) shoplist.reset('down');
            }
        }
    },
    onReachBottom : function(){
        var self = this;
        var pdownget = this.selectComponent('#pdownget');
        if(pdownget) pdownget.load();

        var cityinfo = this.selectComponent('#cityinfo');
        if(cityinfo) cityinfo.load();

        for (var i = 0; i < this.data.pagedata.length; i++) {
            if(this.data.pagedata[i].name == 'article'){
                let articlelist = this.selectComponent('#articlelist'+i);
                if(articlelist) articlelist.load();
            }else if(this.data.pagedata[i].name == 'shop'){
                let shoplist = this.selectComponent('#shop'+i);
                if(shoplist) shoplist.load();
            }
        }
    },
    showJoin(e){
    	var self = this;
    	self.setData({
    		Joinanimation : !self.data.Joinanimation,
    	})
    },
    showsub(e){
        this.setData({
            showsub : true,
            subtype : e.currentTarget.dataset.type
        })
    },
    locCity(){
        var self = this;
        wx.chooseLocation({
            success : function(res){
                wx.setStorageSync('nlloc',{latitude:res.latitude,longitude:res.longitude,name: res.name ? res.name : res.address });
                self.setData({
                    locname : res.name ? res.name : res.address,
                    inited : false,
                    showsub : false,
                })
                self.getData(res.latitude,res.longitude);
            }
        });
    },
    geocoder(lat,lng,key){
        var self = this;
        var QQMapWX = require('../../resource/js/qqmap-wx-jssdk.min.js');
        var qqmapsdk = new QQMapWX({
            key: key // 必填
        });
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: lat,
                longitude: lng
            },
            success: function(res) {
                self.setData({
                    locname : res.result.address_component.city + res.result.address_component.street_number,
                });
            }
        })
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    toweburl : function(e){
        app.com.toweburl( e,this );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    other : function( e ){
        app.com.otherapp( e );
    },
    weburl : function(e){
        app.com.toweburl( e,this );
    },
    url : function( e ){
        app.com.navigateto( e );
    },   
    tel : function(e){
        app.com.callphone( e );
    },
    copy(e){
        wx.setClipboardData({
            data : e.currentTarget.dataset.copy
        })
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    swiperchange : function(e){
        this.animationaa = wx.createAnimation({
            duration:0,
        })
        this.animationaa.opacity(0).step();
        this.setData({
          animationaa: this.animationaa.export()
        })

        this.animationaa = wx.createAnimation({
            duration:2000,
        })
        this.animationaa.opacity(1).step();
        this.setData({
          //输出动画
          animationaa: this.animationaa.export()
        })
    }
})